<script setup>

import router from "@/modules/router.js";
import jing from "@/assets/jing.png";
import {ref} from "vue";
import {homePageQueryArticle} from "@/api/api.js";

const checked = ref(false)
const text = ref("")
const checkedChange = () => {
  pageNo.value = 0
  getHomePageQueryArticle();
}

const search = () => {
  pageNo.value = 0
  getHomePageQueryArticle();
}

const refreshing = ref(false)

const loading = ref(false)

const finished = ref(false)

const pageNo = ref(0)

const articleList = ref([])
const onRefresh = () => {
  refreshing.value = true;
  loading.value = true;
  pageNo.value = 0
  getHomePageQueryArticle();
};
const getHomePageQueryArticle = () => {
  pageNo.value = pageNo.value + 1
  let query = {}
  if(checked.value){
    query = {
      pageNo: pageNo.value,
      pageSize: 10,
      digested: 1 ,
      talkText: text.value
    }
  }else{
    query = {
      pageNo: pageNo.value,
      pageSize: 10,
      talkText: text.value
    }
  }

  homePageQueryArticle(query).then(res => {
    if (pageNo.value == 1) {
      articleList.value = []
      articleList.value.length = 0
      debugger;
      articleList.value = res.result.records
    } else {
      articleList.value.push(...res.result.records)
    }
    if (articleList.value.length == res.result.total) {
      finished.value = true
    }

  }).finally(() => {
    refreshing.value = false
    loading.value = false
  })
}
</script>

<template>
  <van-sticky>
    <div style="background-color: #f2f5f9">
      <div class="header header-search">
        <van-icon name="arrow-left" size="25" @click="router.back()"/>
        <van-field v-model="text" class="inputBorder">
        </van-field>
        <van-button size="small" style="width: 60px" @click="search">搜索</van-button>
      </div>
      <div class="sort">
        <van-switch v-model="checked" @change="checkedChange">
          <template #node>
            <div class="icon-wrapper">
              <van-icon
                  :name="
              checked
                ? jing
                : ''
            "
              ></van-icon>
            </div>
          </template>
        </van-switch>
      </div>
    </div>
  </van-sticky>

  <div class="content-card-list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getHomePageQueryArticle"
      >
        <share-card v-for="(item, index) in articleList" :key="index" :item="item"></share-card>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<style scoped>
.sort {
  height: 50px;
  background-color: #ffffff;
  width: 100%;
  border-radius: 5px;
  margin-top:10px;
  display: flex;
  align-items: center;
  width: 837px;
  margin: auto;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10px;
  border-bottom: 1px solid #cfcfcf;
  background: #ffffff;
  border-radius: 10px;
  width: 837px;
  margin: auto;
}

.content-card-list {
  background: #f2f5f9;
  max-width: 847px;
  margin:auto;
}
.inputBorder {
  margin-left: 20px;
}
</style>

<style>
.icon-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
}
.header-search .van-cell{
  padding: 3px !important;
}
.inputBorder {
  border: 1px solid #d1d1d1;
  border-radius: 3px;
}
.van-switch--on {
  background: #107459;
}
</style>
